package com.chocho.producerserver.service;

import com.chocho.producerserver.bean.Paper;
import com.chocho.producerserver.bean.QCollection;
import com.chocho.producerserver.dao.PaperDao;
import com.chocho.producerserver.utils.JwtUtil;
import com.chocho.producerserver.utils.UUIDUtil;
import com.chocho.producerserver.vo.VCollection;
import com.chocho.producerserver.vo.VPaper;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.sql.Date;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by chocho on 2022/4/12.
 */
@Service
public class PaperService {

    @Autowired
    private PaperDao paperDao;

    public boolean addPaper(VPaper v){
        Paper p = new Paper();
        p.setPaperId(UUIDUtil.getUUID());
        p.setUserId(v.getUserId());
        p.setQuestionId(v.getQuestionId());
        p.setCreateTime(new Date(System.currentTimeMillis()));
        //TODO 后面需要替换成打分系统的评分规则
        p.setPaperScore(0);
        p.setPaperStatus(v.getStatus());
        p.setPaperAnswer(v.getAnswer());
        //TODO 后面需要检测内存和时间
        p.setMemoryLimit(0);
        p.setTimeLimit(0);

        return paperDao.insert(p) > 0;
    }

    public List<VPaper> getPaperByQuery(Map<String, Object> map){
        List<Paper> list = paperDao.getPaperByQuery(map);
        List<VPaper> paperList = new ArrayList<>();
        for(Paper c : list){
            VPaper v = new VPaper();
            v.setId(c.getPaperId());
            v.setUserId("chocho");
            v.setQuestionId(c.getQuestionId());
            v.setCreateTime(c.getCreateTime());
            v.setScore(c.getPaperScore());
            v.setStatus(c.getPaperStatus());
            v.setAnswer(c.getPaperAnswer());
            v.setMemoryLimit(c.getMemoryLimit());
            v.setTimeLimit(c.getTimeLimit());

            paperList.add(v);
        }

        return paperList;
    }

    public List<VPaper> getPaperById(HttpServletRequest request, Map<String, Object> map){
        String token = request.getHeader("token");
        try {
            Claims claims = JwtUtil.parseJWT(token);
            map.put("userId", claims.getSubject());
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException("token非法");
        }
        List<Paper> list = paperDao.getPaperByQuery(map);
        List<VPaper> paperList = new ArrayList<>();
        for(Paper c : list){
            VPaper v = new VPaper();
            v.setId(c.getPaperId());
            v.setUserId("chocho");
            v.setQuestionId(c.getQuestionId());
            v.setCreateTime(c.getCreateTime());
            v.setScore(c.getPaperScore());
            v.setStatus(c.getPaperStatus());
            v.setAnswer(c.getPaperAnswer());
            v.setMemoryLimit(c.getMemoryLimit());
            v.setTimeLimit(c.getTimeLimit());

            paperList.add(v);
        }

        return paperList;
    }
}
